######################################################################
# skypeapi-test-osx.py
#
# This file is part of the pyskype project, please see also
# http://code.google.com/p/pyskype/
#
# This is a proof of concept that wrapping the Skype API using
# Python on Mac OS X using PyObjC does work.
#
# The application does basically:
#
# - load the Skype Framework and construct proxy objects for it
# - defines the informal protocol needed for the delegate which
#   gets the notifications from skype
# - creates a delegate object
# - creates a main loop
# - sets the delegate
# - connects to skype
# - waits
# - sends a "PROTOCOL 1" Command
#
# After we're connected, the Skype API calls our delegate objects
# "skypeAttachResponse_" method, and then we get notifications from the
# API through "skypeNotificationReceived_".
#
# This code assumes a few things:
#
# - pyobjc is installed and working
# - the Skype API is installed in the current user's library
#   directory
#
# (C) 2006 Stefan Eletzhofer <stefan.eletzhofer@googlemail.com>
#
# This code is licensed under the GNU GPL 2.0
#
#
# $Id: skypeapi-test-osx.py 2 2006-10-24 22:55:59Z stefan.eletzhofer $
import sys
import objc
from Foundation import *
from PyObjCTools import AppHelper

# Here the magic happens. This loads the Framework and creates a usable Python
# proxy object for us.
objc.loadBundle( "SkypeAPI", globals(), bundle_path=objc.pathForFramework( "~/Library/Frameworks/Skype.framework" ) )

# fetch the "simple delegate" protocol. See the SkypeAPI.h header which comes
# with the framework.
SkypeAPIDelegate = objc.protocolNamed( "SkypeAPIDelegate" )

# Create a informal protocol for the full delegate with callbacks. Without
# this class (?) as a mixin to the delegate class below we get nice segmentation
# faults when Skype tries to send us a message. The "signature" stuff is documented
# here: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html
SkypeAPIDelegateInformalProtocol = objc.informal_protocol(
    "SkypeAPIDelegateInformalProtocol",
    [
#- (void)skypeNotificationReceived:(NSString*)aNotificationString;
        objc.selector(
            None,
            selector='skypeNotificationReceived:',
            signature='v@:@',
            isRequired=0,
        ),
#- (void)skypeAttachResponse:(unsigned)aAttachResponseCode;				// 0 - failed, 1 - success
        objc.selector(
            None,
            selector='skypeAttachResponse:',
            signature='v@:I',
            isRequired=0,
        ),
#- (void)skypeBecameAvailable:(NSNotification*)aNotification;
        objc.selector(
            None,
            selector='skypeBecameAvailable:',
            signature='v@:@',
            isRequired=0,
        ),
#- (void)skypeBecameUnavailable:(NSNotification*)aNotification;
        objc.selector(
            None,
            selector='skypeBecameUnavailable:',
            signature='v@:@',
            isRequired=0,
        ),
    ]
)

class Delegate(NSObject, SkypeAPIDelegateInformalProtocol):
    """
    A delegate whcih gets the notifications from Skype.
    """
    def init(self, app_name):
        self = super( Delegate, self).init()
        if self is None:
            return None

        self._appname = app_name

        return self

    def clientApplicationName( self ):
        return self._appname

    def skypeAttachResponse_( self, code ):
        NSLog( "skypeAttachResponse: %d" % ( code ) )

    def skypeNotificationReceived_( self, nsstring ):
        NSLog( "skypeNotificationReceived: %s " % ( nsstring ) )

    def skypeBecameAvailable_( self, notification ):
        NSLog( "skypeBecameAvailable: %s " % ( notification ) )

    def skypeBecameUnavailable_( self, notification ):
        NSLog( "skypeBecameUnavailable: %s" % ( notification ) )

delegate = Delegate.alloc().init( "python test app" ).retain()

def do_connect():
    global delegate
    NSLog( "connecting to skype" )
    SkypeAPI.setSkypeDelegate_( delegate )
    SkypeAPI.connect()

    NSLog( "waiting ...." )
    AppHelper.callLater( 10, do_send )

def do_send():
    NSLog( "sending" )
    answer = SkypeAPI.sendSkypeCommand_( u"PROTOCOL 1" )
    NSLog( "got answer: %s" % ( answer ) )

def main():
    NSLog( "running event loop" )
    AppHelper.callLater( 1, do_connect )
    AppHelper.runConsoleEventLoop( installInterrupt=True )

if __name__ == "__main__":
    main()

# vim: set ts=4 sw=4 expandtab :
